package com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.test;

import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.Quackable;
import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.composite.Flock;
import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.decorator.QuackCounter;
import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.factory.AbstractDuckFactory;
import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.factory.CountingDuckFactory;
import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.goose.Goose;
import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.goose.GooseAdapter;
import com.suxiaoxiao.springbootlearn.designpattern.compoundpattern.observer.Quackologist;

/**
 * 鸭子模拟器
 * @author 、
 */
public class DuckSimulator {

    public static void main(String[] args) {
        DuckSimulator duckSimulator = new DuckSimulator();
        // 与装饰器结合的鸭子工厂
        AbstractDuckFactory countingDuckFactory = new CountingDuckFactory();
        duckSimulator.simulate(countingDuckFactory);
    }

    void simulate(AbstractDuckFactory abstractDuckFactory) {
        Quackable mallardDuck = abstractDuckFactory.createMallardDuck();
        Quackable redheadDuck = abstractDuckFactory.createRedheadDuck();
        Quackable duckCall = abstractDuckFactory.createDuckCall();
        Quackable rubberDuck = abstractDuckFactory.createRubberDuck();
        // 适配器
        Quackable gooseDuck = new GooseAdapter(new Goose());

        System.out.println("组合模式，鸭群！");

        Flock flockOfDucks = new Flock();

        flockOfDucks.add(redheadDuck);
        flockOfDucks.add(duckCall);
        flockOfDucks.add(rubberDuck);

        Flock flockOfMallards = new Flock();

        // 创建一群绿头鸭
        Quackable mallardDuck1 = abstractDuckFactory.createMallardDuck();
        Quackable mallardDuck2 = abstractDuckFactory.createMallardDuck();
        Quackable mallardDuck3 = abstractDuckFactory.createMallardDuck();
        Quackable mallardDuck4 = abstractDuckFactory.createMallardDuck();

        flockOfMallards.add(mallardDuck);
        flockOfMallards.add(mallardDuck1);
        flockOfMallards.add(mallardDuck2);
        flockOfMallards.add(mallardDuck3);
        flockOfMallards.add(mallardDuck4);

        flockOfDucks.add(flockOfMallards);

        System.out.println("观察者模式");
        Quackologist quackologist = new Quackologist();
        flockOfDucks.registerObserver(quackologist);

        // 一群鸭开始叫！
        simulate(flockOfDucks);

        System.out.println("鸭子一共叫了" + QuackCounter.getNumberOfQuacks() + "声");

    }

    void simulate(Quackable quackable) {
        quackable.quack();
    }

}
